package com.ocom.srvpaccess.controller;

import com.ocom.common.constant.SrvpAccessDeviceUrl;
import com.ocom.common.entity.access.EntityMjDeviceInfo;
import com.ocom.common.entity.access.EntityMjZoneDate;
import com.ocom.common.enums.MachEnum;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.utils.RespUtilMach;
import com.ocom.srvpaccess.service.MjDeviceInfoService;
import com.ocom.srvpaccess.service.ZoneDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ZoneTimeController {
    @Autowired
    private MjDeviceInfoService mjDeviceInfoService;

    @Autowired
    private ZoneDateService zoneDateService;

    // 获取门禁时区表（getZoneTime）
    @GetMapping(value = SrvpAccessDeviceUrl.URLP_ACCESSDEVICE_getZoneTime)
    public RespMachMessage getZoneTime(Long companyId, String machNo){

        //判断参数
        if (companyId <=0 || machNo==null){
            return RespUtilMach.error(MachEnum.PARAMS_ERROR);
        }
        //1、修改门禁设备表mj_device_info中com_id、device_no匹配的记录：
        EntityMjDeviceInfo deviceInfo = mjDeviceInfoService.getDeviceInfo(companyId, machNo);
        if (deviceInfo.getZoneVerLast() == null){
            //zone_ver_last = zone_ver
            mjDeviceInfoService.updateZoneVerLast(companyId,machNo,deviceInfo.getZoneVer());
        }else {
            if (!deviceInfo.getZoneVerLast().equals(deviceInfo.getZoneVer())){
                //zone_ver_last = zone_ver
                mjDeviceInfoService.updateZoneVerLast(companyId,machNo,deviceInfo.getZoneVer());
            }
        }

//        2、从分库门禁时区设置表mj_zone_date查找满足条件的记录：
//        select date , zone_no from mj_zone_date
//        where com_id = companyID
//        and ( day_date = 0 or day_date = 1 and date >= 当前日期 )
//        order by date
        List<EntityMjZoneDate> zoneTime = zoneDateService.getZoneTime(companyId);
        List<Map<String,Object>> zoneTimeList = new ArrayList<>();
        for (EntityMjZoneDate entityMjZoneDate: zoneTime) {
            Map<String, Object> map = new HashMap<>();
            map.put("date",entityMjZoneDate.getDate());
            map.put("timeZone",entityMjZoneDate.getZoneNo());
            zoneTimeList.add(map);
        }
        return RespUtilMach.success(zoneTimeList);
    }

    // 更改时区表状态（changeZoneTimeState）
    @PostMapping(value = SrvpAccessDeviceUrl.URLP_ACCESSDEVICE_changeZoneTimeState)
    public RespMachMessage changeZoneTimeState(Long companyId, String machNo){

        //判断参数
        if (companyId <=0 || machNo==null){
            return RespUtilMach.error(MachEnum.PARAMS_ERROR);
        }
        //1、修改门禁设备表mj_device_info中com_id、device_no匹配的记录：
        EntityMjDeviceInfo deviceInfo = mjDeviceInfoService.getDeviceInfo(companyId, machNo);
        if (deviceInfo.getZoneVerTab() == null){
            //zone_ver_tab = zone_ver_last
            try {
                mjDeviceInfoService.updateZoneVerTab(companyId,machNo,deviceInfo.getZoneVerLast());
            }catch (Exception exception){
                exception.printStackTrace();
            }
        }else {
            if (!deviceInfo.getZoneVerTab().equals(deviceInfo.getZoneVerLast())){
                //zone_ver_tab = zone_ver_last
                try {
                    mjDeviceInfoService.updateZoneVerTab(companyId,machNo,deviceInfo.getZoneVerLast());
                }catch (Exception exception){
                    exception.printStackTrace();
                }

            }
        }
        Map<String, Object> map = new HashMap<>();
        //标识本次通讯
        map.put("flag", 0);
        return RespUtilMach.success(map);
    }

}
